<?php
//模型
class CourseModel extends MY_Model {
	function __construct(){
		parent::__construct();
		$this->table = 'course';
	}
	//分页列表
	function get_list($arr=array(),$page=1,$size=10){
		$where = " where c.status = 1 ";
		//课程id
		if(isset($arr['course'])){
			$where .= " AND c.id in ({$arr['course']}) ";
		}		
		
		$sql  = "select c.id,c.name,c.title,c.type_id,c.theme_img,c.nandu,ranzhi,c.duration,m.nickname as coach_man,s.name as shop_name from 
				".$this->db->dbprefix.$this->table." as c left join 
				".$this->db->dbprefix."coach as m on c.cid=m.id left join 
				".$this->db->dbprefix."shop as s on c.shop_id=s.id 
				
				{$where} LIMIT ".($page-1)*$size .",{$size}";
				
		$query = $this->db->query($sql);
		$res = $query->result_array();
		
		$this->load->model('Index/CourseCollectionModel', 'collect', 'index');
		foreach($res as $k=>$v){
			$res[$k]['is_collect'] = 0;
			if(isset($arr['user_id'])){
				$cinfo = $this->collect->get_info($arr['user_id'],$v['id']);
				$res[$k]['is_collect'] = $cinfo?1:0;
			}
			if($v['type_id']==0){
				$res[$k]['type_name'] = "私教";
			}else{
				$res[$k]['type_name'] = "团教";
			}			
		}
		
		$sql  = "select count(*) as num from ".$this->db->dbprefix.$this->table." as c ".$where;
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return array('list'=>$res,'count' =>$row['num']);		
	}
	//全部列表
	function get_lists($arr=array()){
		$where = " where 1 ";
		//课程id
		if(isset($arr['course'])){
			$where .= " AND c.id in ({$arr['course']}) ";
		}		
		
		$sql  = "select c.id,c.name,c.title,c.type_id,c.theme_img,c.nandu,ranzhi,c.duration,m.nickname as coach_man,s.name as shop_name from 
				".$this->db->dbprefix.$this->table." as c left join 
				".$this->db->dbprefix."coach as m on c.cid=m.id left join 
				".$this->db->dbprefix."shop as s on c.shop_id=s.id 				
				{$where}  ";		
		$query = $this->db->query($sql);
		$res = $query->result_array();
				
		foreach($res as $k=>$v){
			if($v['type_id']==0){
				$res[$k]['type_name'] = "私教";
			}else{
				$res[$k]['type_name'] = "团教";
			}
		}
		
		return $res;		
	}
	//详情
	function get_info($id,$user_id=0){
		$sql  = "select c.*,m.nickname as coach_man,s.name as shop_name from 
				".$this->db->dbprefix.$this->table." as c left join 
				".$this->db->dbprefix."coach as m on c.cid=m.id left join
				".$this->db->dbprefix."shop as s on c.shop_id=s.id 	
				where c.id='{$id}' ";
		
		$query = $this->db->query($sql);
		$row = $query->row_array();
		if(empty($row)){
			return false;
		}
		
		
		$this->load->model('Index/SportModel','sport');
		
		$sport    = array();
		$tool 	  = array();
		$suitable = array();
		
		if($row['sport']){
			$sport_id = explode(",",$row['sport']);				
			foreach($sport_id as $v){
				$info = $this->sport->get_info($v);
				if($info){
					$sport[] = $info;
					
				}
				
			}
			$row['sport'] = $sport;
		}
		if($row['tool']){
			$row['tool'] = json_decode($row['tool'],true);
		}
		if($row['suitable']){
			$row['suitable'] = json_decode($row['suitable'],true);
		}
		
		if($row['type_id']==0){
			$row['type_name'] = "私教";
		}else{
			$row['type_name'] = "团教";
		}
		
		$row['is_collect'] = 0;
		if(isset($user_id)){
			$this->load->model('Index/CourseCollectionModel', 'collect', 'index');
			$cinfo = $this->collect->get_info($user_id,$row['id']);
			$row['is_collect'] = $cinfo?1:0;
		}
			
		return $row;
	}
	//唯一条件获取详情
	function unique_get_info($field,$value){
		$sql  = "select * from ".$this->db->dbprefix.$this->table." where {$field}='{$value}' ";
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return $row;
	}
	
}

